
//
//  QGUitility.m
//  qeegooChat
//
//  Created by heew on 16/1/7.
//  Copyright © 2016年 com.autozi. All rights reserved.
//

#import "QGUitility.h"
#define GJCFDispatchOnce(onceToken,block) [QGUitility dispatchOnce:onceToken action:block]
@implementation QGUitility

+ (BOOL)directoryExist:(NSString*)dirPath {
    
    if ([self stringIsNull:dirPath]) {
        return NO;
    }
    BOOL isDir = YES;
    
    return [[self fileManager] fileExistsAtPath:dirPath isDirectory:&isDir];
}
+ (NSFileManager *)fileManager
{
    return [NSFileManager defaultManager];
}
+ (BOOL)createDirectory:(NSString*)dirPath {
    if ([self isNullObject:dirPath]) {
        return NO;
    }
    if ([self stringIsNull:dirPath]) {
        return YES;
    }
    return [[self fileManager] createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil];
}
+ (BOOL)stringIsNull:(NSString *)string
{
    if (![string isKindOfClass:[NSString class]]) {
        return YES;
    }
    
    if (!string || [string isKindOfClass:[NSNull class]] || string.length == 0 || [string isEqualToString:@""]) {
        return YES;
    }else{
        return NO;
    }
}
+ (BOOL)isNullObject:(id)anObject
{
    if (!anObject || [anObject isKindOfClass:[NSNull class]]) {
        return YES;
    }else{
        return NO;
    }
}
+ (void)userDefaultCache:(id<NSCoding>)value key:(id)key {
    
    if ([self isNullObject:key] || [self isNullObject:value]) {
        return;
    }
    [[self standDefault]setObject:value forKey:key];
}

+ (id)userDefaultGetValue:(id)key {
    
    if ([self isNullObject:key]) {
        return nil;
    }
    return [[self standDefault] objectForKey:key];
}
+ (NSUserDefaults *)standDefault
{
    return [NSUserDefaults standardUserDefaults];
}
+ (void)postNoti:(NSString *)notiName
{
    [self postNoti:notiName withObject:nil];
}

+ (void)postNoti:(NSString *)notiName withObject:(id)obj
{
    [self postNoti:notiName withObject:obj withUserInfo:nil];
}

+ (void)postNoti:(NSString *)notiName withObject:(id)obj withUserInfo:(NSDictionary *)infoDict
{
    if ([self stringIsNull:notiName]) {
        return;
    }
    [QGNotificationCenter postNotificationName:notiName object:obj userInfo:infoDict];
}
+ (NSString*)currentTimeStampString
{
    NSDate *now = [NSDate date];
    NSTimeInterval timeInterval = [now timeIntervalSinceReferenceDate];
    
    NSString *timeString = [NSString stringWithFormat:@"%lf",timeInterval];
    timeString = [timeString stringByReplacingOccurrencesOfString:@"." withString:@""];
    
    return timeString;
    
}
+ (BOOL)fileExist:(NSString*)path
{
    if ([self stringIsNull:path]) {
        return NO;
    }
    return [[self fileManager] fileExistsAtPath:path];
}
+ (void)systemMemoryCacheSet:(id<NSCoding>)value key:(id)key
{
    if ([self isNullObject:key] || [self isNullObject:value]) {
        return;
    }
    [[self shareCache]setObject:value forKey:key];
}
+ (NSCache *)shareCache
{
    static NSCache *_gjcfNSCacheInstance = nil;
    static dispatch_once_t onceToken;
    
    GJCFDispatchOnce(onceToken, ^{
        
        if (!_gjcfNSCacheInstance) {
            _gjcfNSCacheInstance = [[NSCache alloc]init];
        }
        
    });
    
    return _gjcfNSCacheInstance;
}
+ (void)dispatchOnce:(dispatch_once_t)onceToken action:(dispatch_block_t)block
{
    if(!block){
        return;
    }
    dispatch_once(&onceToken, block);
}
@end
